﻿using System;
using System.Collections;
using System.Collections.Generic;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    
    public class AddMonsterGroup : LatentActionNodeBase
    {

        [Serializable]
        public class Monster
        {
            //public int uniqueId;
            [ParadoxNotion.Design.Name("角色配置ID(对应rpg_npc配表)")]
            public int npcId;               // npcId 对应 rpg_npc配表
            [ParadoxNotion.Design.Name("街道Npc唯一ID")]
            public int streetNpcId;         // 街道npc的唯一Id
            [ParadoxNotion.Design.Name("是否随机出生点")]
            public bool randomBorn = false;
            [ParadoxNotion.Design.Name("出生点")]
            public string pointName;
            [ParadoxNotion.Design.Name("被击杀增加计数器名称")]
            public string counterName;
            [ParadoxNotion.Design.Name("动画状态")]
            public string animState;
            [ParadoxNotion.Design.Name("所属阵营")]
            public SideType sideType;
            [ParadoxNotion.Design.Name("掉落包ID")]
            public int dropItemPackId;
            [ParadoxNotion.Design.Name("重生点")]
            public string rebornPoint;
            [ParadoxNotion.Design.Name("是否是boss")]
            public bool isBoss;
            [ParadoxNotion.Design.Name("行为树ID")]
            public int graphId = 1002;
            [ParadoxNotion.Design.Name("最低生命值(用于锁血)")]
            public int minHp = 0;

            [ParadoxNotion.Design.Name("是否是巡逻NPC")]
            public bool isPatrol;

            [ParadoxNotion.Design.Name("巡逻Npc的路径ID")]
            public int PatrolWayId;

            [ParadoxNotion.Design.Name("巡逻Npc的速度")]
            public float PatrolSpeed = 0.2f;
        }

        public BBParameter<List<AddMonsterGroup.Monster>> monsterConfigs;

        protected List<AIBirthData> _aiBirthDataList;

        public IEnumerator Invoke()
        {
            _aiBirthDataList = new List<AIBirthData>();
            if (monsterConfigs.value != null)
            {
                foreach (var monsterConfig in monsterConfigs.value)
                {
                    BluePrintPoint point;
                    if (!monsterConfig.randomBorn)
                    {
                        point = BlueprintUtils.GetBlueprintPoint(monsterConfig.pointName);
                    }
                    else
                    {
                        point= BlueprintUtils.GetRandomBlueprintPoint();
                    }
                    if (point != null)
                    {
                        AIBirthData birthData = new AIBirthData();
                        birthData.uniqueId = NpcUniqueIdUtils.GetEventMonsterUniqueID();
                        birthData.position = point.position;
                        birthData.forward = point.forward;
                        birthData.modelResId = monsterConfig.npcId;
                        birthData.animState = "";
                        birthData.sideType = monsterConfig.sideType;
                        birthData.isBoss = monsterConfig.isBoss;
                        birthData.isStreetEvent = true;
                        birthData.isStatic = false;
                        birthData.dropItemPackId = monsterConfig.dropItemPackId;
                        birthData.rebornPoint = monsterConfig.rebornPoint;
                        birthData.isPatrol = monsterConfig.isPatrol;
                        birthData.PartrolWayId = monsterConfig.PatrolWayId;
                        birthData.PatrolSpeed = monsterConfig.PatrolSpeed;
                        LibertyAIUtils.AIManager.AddNpc(birthData, monsterConfig.graphId);
                        _aiBirthDataList.Add(birthData);
                        
                        // 切换动画状态 monsterConfig.animState
                        var aiEntity = LibertyAIUtils.AIManager.NpcBirthManager.GetNpc(birthData.uniqueId);
                        if (aiEntity)
                        {
                            if (!string.IsNullOrEmpty(monsterConfig.animState))
                            {
                                aiEntity.viewer.animationAgent.PlayStreetEventDynamicAnimation(monsterConfig.animState);
                            }
                            aiEntity.data.minHp = monsterConfig.minHp;
                        }
                        StreetEventManager.GetInstance("StreetEventManager").OnAddMonster(birthData.uniqueId,monsterConfig.streetNpcId,monsterConfig.counterName,birthData.sideType);
                        yield return new WaitForOneFrame();
                    }
                }
            }
        }
        
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            node.AddFlowInput("In", (f) => { Begin(Invoke(), f); });
        }

        public override void OnGraphStoped()
        {
            base.OnGraphStoped();
            if (_aiBirthDataList!=null)
            {
                foreach (var birthData in _aiBirthDataList)
                {
                    if (LibertyAIUtils.AIManager)
                    {
                        LibertyAIUtils.AIManager.RemoveNpc(birthData.uniqueId);
                    }
                }
            }
        }
    }
}